// 打开对话
function openChat(btn){
    var session_sn = $(btn).attr('data-sn');

    $("#chatContactTab .contacts-item").removeClass('active');
    $(btn).addClass('active');
    $("#initChats").hide();
    $(".main").addClass("main-visible");

    // $("#chats-content").removeClass('active'); // 手机端样式
    

    $.ajax({
        url:api_path+'msg/index',
        data:{session_sn:session_sn},
        type:'post',
        dataType:'json',
        success:function(response){
            global_assistant = response.assistant;
            global_assistant.session_sn = session_sn;
            renewAssistant();

            global_wait_msgs = response.wait_msgs;

            var list = response.list;
            var str = '';
            str += createMsgHtml(global_assistant.greet,1,response.session.ctime);
            for(var i in list){
                str += '<div class="message-day">';
                str += '<div class="message-divider sticky-top pb-2" data-label="'+i+'">&nbsp;</div>';
                        
                for (var j in list[i]) {
                    var data = list[i][j];
                    if (data.type == 'text') {
                      str += createMsgHtml(data.message,0,data.ctime);  
                      str += createMsgHtml(data.reply,1,data.rtime,data.id);
                    }else if(data.type == 'htsc'){
                      str += createHtscMessageHtml(data.message,data.ctime);
                      str += createHtscReplyHtml(data.reply,data.rtime);
                    }else if(data.type == 'aidraw'){
                      str += createAidrawMessageHtml(data.message,data.ctime);
                      str += createAidrawReplyHtml(data.reply,data.rtime);
                    }
                }
                str += '</div>';
            }
            $("#chatContent").html(str);
            $("#msgChats").show();
            scrollBottom();
        }
    });
}

// 更新头部助理信息
function renewAssistant(){
    $(".chat-header .media-body").find('h6').html(global_assistant.name);
    $(".chat-header .media-body").find('small').html(global_assistant.description);
    $(".chat-header .media-body").find('small').attr('title',global_assistant.description);
    $(".chat-header .injectable").attr('src',upload_path+'/'+global_assistant.image);
}

// 关闭session
function closeSession(){
    $("#chatContactTab .contacts-item.active").removeClass('active');
    $("#initChats").show();
    $("#msgChats").hide();
}

// 清空session
function clearSession(){
    if (!confirm('确定清空？')) {
        return false;
    }
    $.ajax({
        url:api_path+'session/clear',
        data:{session_sn:global_assistant.session_sn},
        type:'post',
        dataType:'json',
        success:function(data){
            if (data.code == 2000) {
                cchUtil.message('清除成功');
                $("#chatContactTab .contacts-item.active").trigger('click');
            }else{
                cchUtil.message(data.msg,'danger');
            }
        }
    });
}

// 删除session
function deleteSession(){
    if (!confirm('确定删除？')) {
        return false;
    }
    $.ajax({
        url:api_path+'session/delete',
        data:{session_sn:global_assistant.session_sn},
        type:'post',
        dataType:'json',
        success:function(data){
            if (data.code == 2000) {
                cchUtil.message('删除成功');
                $("#chatContactTab .contacts-item.active").remove();
                $("#initChats").show();
                $("#msgChats").hide();
            }else{
                cchUtil.message(data.msg,'danger');
            }
        }
    });
}

// 初始化session
function initeCreateSession(){
    var assistant_id = $(".init_assistant.active").attr('data-id');
    if (assistant_id <= 0) {
        cchUtil.message('请选择助理','danger');
    }
    createSession(assistant_id);
}

// 删除session - 手机界面
function deleteSessionMobile(){
    if (!confirm('确定删除？')) {
        return false;
    }
    $.ajax({
        url:api_path+'session/delete',
        data:{session_sn:global_assistant.session_sn},
        type:'post',
        dataType:'json',
        success:function(data){
            if (data.code == 2000) {
                cchUtil.message('删除成功');
                $("#chatContactTab .contacts-item.active").remove();
                $(".close_btn_mobile").trigger('click');
            }else{
                cchUtil.message(data.msg,'danger');
            }
        }
    });
}

// 创建一个新的session
function createSession(assistant_id){
    $.ajax({
        url:api_path+'session/save',
        data:{assistant_id:assistant_id},
        type:'post',
        dataType:'json',
        success:function(data){
            var str = '<li class="contacts-item friends" onclick="openChat(this)" data-sn="'+data.session_sn+'">'+
                    '<a class="contacts-link" href="javascript:void(0);">'+
                        '<div class="avatar avatar-online">'+
                            '<img src="'+upload_path+'/'+data.assistant.image+'" alt="">'+
                        '</div>'+
                        '<div class="contacts-content">'+
                            '<div class="contacts-info">'+
                                '<h6 class="chat-name text-truncate">'+data.assistant.name+'</h6>'+
                                '<div class="chat-time">'+data.msg.ctime+'</div>'+
                            '</div>'+
                            '<div class="contacts-texts">'+
                                '<p class="text-truncate">'+data.msg.message+'</p>'+
                            '</div>'+
                        '</div>'+
                    '</a>'+
                '</li>';
            $("#chatContactTab").prepend(str);
            // $('.contacts-list .contacts-link').on('click',function(){$(".main").addClass("main-visible")})
            // console.log($("#chatContactTab").find('li')[0])
            $($("#chatContactTab").find('li')[0]).trigger('click');
            // openChat2(data.session_sn)
        }
    });
}